/**
 * @author 徐楠
 * @date 2022/4/24 22:17
 * @version 1.0
 */

package com.xunan.likou;

import java.util.HashMap;
import java.util.Map;

public class RansomLetter {
    public static void main(String[] args) {
        boolean b = canConstruct( "aa","ab");
        System.out.println(b);

    }

    public static boolean canConstruct(String ransomNote, String magazine) {

        char[] magazineTOChars = magazine.toCharArray();
        Map<Character, Integer> map = new HashMap<>();
        for (char aChar : magazineTOChars) {
            map.put(aChar, map.getOrDefault(aChar, 0) + 1);
        }

        char[] ransomNoteToChars = ransomNote.toCharArray();
        for (char ransomNoteToChar : ransomNoteToChars) {
            Integer temp = map.get(ransomNoteToChar);
            if (temp == null || temp == 0) {
                return false;
            }
            map.put(ransomNoteToChar, temp - 1);
        }
        return true;
    }
}
